//
//  BRStationsDetailViewController.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/9/22.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRStationsDetailViewController.h"
#import "BRStationDetailHeadView.h"
#import "BRStationImgCollectionViewCell.h"
#import "GXJWebViewController.h"

#import "TZImagePreviewController.h"

@interface BRStationsDetailViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,SDCycleScrollViewDelegate>
{
    UICollectionView *_myCollectionView;
    UIView *_contentView;
    UIImageView *_headImg;
    QMUIMarqueeLabel *_titleL;
    QMUILabel *_locL;
    QMUILabel *_availableL;
    QMUILabel *_emptyL;
//    QMUILabel *_pricePlanL;
    QMUILabel *_timeLabel;
    QMUIButton *_locBtn;
    UIView *_photoView;
    UIView *_line2;
    UIView *_line3;
    UIStackView *_stackView;
    QMUILabel *_contactLabel;  // 联系方式
}

@property (nonatomic,strong) SDCycleScrollView *sdCyView;

@property (nonatomic,strong) NSMutableArray *imgDatas;

@end

@implementation BRStationsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUI];
    [self request];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear");
    
}

- (void)request {
    
    // 使用新接口 V2
    if (!self.dto || self.dto.stationId == 0) {
        NSLog(@"⚠️ 站点信息不完整，无法请求详情");
    return;
    }
    
    // 获取距离（从 dto.distance 获取，如果为0则使用传入的距离）
    CGFloat distance = self.dto.distance > 0 ? self.dto.distance : 0;
    
    // 如果 distance 为 0，尝试从传入的经纬度计算（如果有的话）
    if (distance == 0 && self.lat != 0 && self.lng != 0 && self.dto.lat != 0 && self.dto.lng != 0) {
        // 简单计算距离（粗略估算）
        CGFloat latDiff = self.dto.lat - self.lat;
        CGFloat lngDiff = self.dto.lng - self.lng;
        distance = sqrt(latDiff * latDiff + lngDiff * lngDiff) * 111.0; // 转换为公里（粗略）
    }
    
    NSString *urlStr = [BRAPI stationDetailV2:self.dto.stationId distance:distance];
    
    [BRHud showInView:self.view];
    [[BRRequestManager shared] GET:urlStr params:nil succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
        [BRHud hiddenInView:self.view];
        
        NSDictionary *data = dataDict[@"data"];
        if (!data) {
            NSLog(@"⚠️ 接口返回数据格式错误");
            return;
        }
        
        // 解析站点详细信息
        BRStationListModel *dto = [BRStationListModel mj_objectWithKeyValues:data];
        
        // 处理 graphInfo 字段（仓位数信息）
        NSDictionary *graphInfo = data[@"graphInfo"];
        if (graphInfo) {
            dto.depotBorrow = [graphInfo[@"depotBorrow"] integerValue];
            dto.depotEmpty = [graphInfo[@"depotEmpty"] integerValue];
            dto.open = [graphInfo[@"open"] boolValue];
            dto.isEmpty = [graphInfo[@"empty"] boolValue];
            dto.isBorrow = [graphInfo[@"borrow"] boolValue];
        }
        
        // 处理经纬度（优先使用 map_latitude 和 map_longitude）
        if ([data[@"map_latitude"] isKindOfClass:[NSNumber class]]) {
            dto.lat = [data[@"map_latitude"] floatValue];
        }
        if ([data[@"map_longitude"] isKindOfClass:[NSNumber class]]) {
            dto.lng = [data[@"map_longitude"] floatValue];
        }
        
        // 处理背景图片（background 字段是完整URL）
        NSString *background = data[@"background"];
        if (background && background.length > 0) {
            // 将 background 作为图片URL使用
            dto.images = background;
        }
        
        // 处理距离文本（如果有）
        NSString *distanceText = dataDict[@"distanceText"];
        if (distanceText && distanceText.length > 0) {
            // 解析距离文本（例如 "0.8 Km"）转换为数值
            NSArray *parts = [distanceText componentsSeparatedByString:@" "];
            if (parts.count > 0) {
                NSString *distanceStr = parts[0];
                CGFloat parsedDistance = [distanceStr floatValue];
                dto.distance = parsedDistance;
            }
        }
        
        // 更新模型数据
        self.dto = dto;
        
        // 更新UI
        [self updateData:dto];
        
        // 更新联系方式显示（使用 mobile 字段）
        NSString *mobile = dto.mobile ?: @"";
        if (mobile.length > 0) {
            self->_contactLabel.text = mobile;
        } else {
            self->_contactLabel.text = @"--";
        }
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        [BRHud hiddenInView:self.view];
        NSLog(@"⚠️ 请求站点详情失败: %@", errMsg);
    }];
}

#pragma mark - Action --
- (void)iconTouch:(QMUIButton *)btn {
    if (btn.tag == 101) {

        BRCustiomAlertView *alertV = [[BRCustiomAlertView alloc] initWithTitle:Localized(@"public_notice") subTitle:Localized(@"Error_visitWebsite") cancelStr:Localized(@"public_cancel") succStr:Localized(@"profile_confirm") cancelAction:^{
            
        } successAction:^{
            [BROpenURL openURL:self.dto.website];
        } showBC:YES];
        
        [self.view addSubview:alertV];
        
        
    }else {

        //电话
        [BROpenURL callPhone:self.dto.landlinePhone];
    }
}

- (void)toMap {
    
    CLLocationCoordinate2D myL = CLLocationCoordinate2DMake(self.lat, self.lng);
    CLLocationCoordinate2D toL = CLLocationCoordinate2DMake(self.dto.lat, self.dto.lng);
    [BROpenURL openMap:myL toLocation:toL titleAddress:self.dto.address];
}

- (NSMutableArray *)imgDatas {
    
    if (_imgDatas == nil) {
        _imgDatas = [NSMutableArray array];
    }
    
    return _imgDatas;
}

- (void)setDto:(BRStationListModel *)dto {
    _dto = dto;
//    NSMutableAttributedString *

}

- (void)updateData:(BRStationListModel *)dto {
    
    // 使用新接口字段（兼容方法会自动处理）
    _titleL.text = dto.branchName;  // 会优先使用 name
    _locL.text = dto.address ?: @"";
    
    // 距离显示 - 新接口V2的distance是公里（浮点数），直接使用公里显示
    NSString *distanceStr;
    if (dto.distance < 1.0) {
        // 小于1公里，显示小数点后2位
        distanceStr = [NSString stringWithFormat:@"%.2f km", dto.distance];
    } else if (dto.distance < 10.0) {
        // 1-10公里，显示小数点后1位
        distanceStr = [NSString stringWithFormat:@"%.1f km", dto.distance];
    } else {
        // 大于10公里，显示整数
        distanceStr = [NSString stringWithFormat:@"%.0f km", dto.distance];
    }
    [_locBtn setTitle:distanceStr forState:UIControlStateNormal];
    
    // 使用新接口的字段
    _availableL.text = [NSString stringWithFormat:@"%@: %ld",Localized(@"home_rent_tag"), (long)dto.depotBorrow];
    _emptyL.text = [NSString stringWithFormat:@"%@: %ld",Localized(@"home_return_tag"), (long)dto.depotEmpty];
    _timeLabel.attributedText = [self openAttString:dto.openTime ?: dto.open_time ?: @""];
    
    [self isOutOfLine:dto.open ? 1 : 0];  // 使用新接口的open字段
    
    // 处理图片显示 - 新接口的 background 字段是完整URL
    if (!BR_StringIsEmpty(dto.images)) {
        [self.imgDatas removeAllObjects];
        
        // 判断是完整URL还是相对路径
        if ([dto.images hasPrefix:@"http://"] || [dto.images hasPrefix:@"https://"]) {
            // 完整URL，直接使用
            [self.imgDatas addObject:dto.images];
        } else {
            // 相对路径，可能是逗号分隔的多个图片，或者是单个图片路径
            NSArray *imgs = [dto.images componentsSeparatedByString:@","];
            NSString *fileS = USERManager.filesSever;
            
        for (int i = 0; i < imgs.count; i++) {
                NSString *imgPath = [imgs[i] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                if (imgPath.length > 0) {
                    NSString *imgUrl = [NSString stringWithFormat:@"%@%@", fileS, imgPath];
            [self.imgDatas addObject:imgUrl];
        }
            }
        }
        
        if (self.imgDatas.count > 0) {
        self.sdCyView.imageURLStringsGroup = self.imgDatas;
        } else {
            self.sdCyView.localizationImageNamesGroup = @[kDefaultImg];
        }
    } else {
        self.sdCyView.localizationImageNamesGroup = @[kDefaultImg];
    }
//    if (!BR_StringIsEmpty(dto.images)) {
//        NSArray *imgs = [dto.images componentsSeparatedByString:@","];
//
//        [self.imgDatas removeAllObjects];
//        for (int i = 0; i < imgs.count; i++) {
//            NSString *imgUrl = [NSString stringWithFormat:@"%@%@",fileS,imgs[i]];
//            [self.imgDatas addObject:imgUrl];
//            if (i == 0) {
//                [_headImg sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:UIImage.qd_stationDefaultImg];
//            }
//        }
//        [_myCollectionView reloadData];
//        _photoView.hidden = NO;
//        _line3.hidden = NO;
//        [_line3 mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self->_line2.mas_bottom).offset(140);
//        }];
//    }else {
//        _photoView.hidden = YES;
//        _line3.hidden = YES;
//        [_line3 mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self->_line2.mas_bottom).offset(-0.5);
//        }];
//    }
//    [_myCollectionView  layoutSubviews];
}


- (void)isOutOfLine:(NSInteger)isOn {
    _emptyL.hidden  = isOn == 1 ? NO : YES;
    if (isOn == 1) {
        _titleL.textColor = kColorTitle;
        _availableL.textColor = kColorMain;
        
        return;
    }
    _titleL.textColor = kColorTitle888;
    _availableL.textColor = kColorTitle888;
    _availableL.text = Localized(@"home_outOfService");
}

- (NSAttributedString *)openAttString:(NSString *)openTime {
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:openTime];
    if ([openTime containsString:@"Open"]) {
        //如果包含了open
        NSRange range = [BRTools rangeof:@"Open" toStr:openTime];
        
        [attStr addAttribute:NSForegroundColorAttributeName value:kColorMain range:range];
        return attStr;
        
    }else {
        if ([openTime containsString:@"Closed"]) {
            NSRange range = [BRTools rangeof:@"Closed" toStr:openTime];
            
            [attStr addAttribute:NSForegroundColorAttributeName value:kColorTitle888 range:range];
            return attStr;
        }
    }
    
    return attStr;
}

#pragma mark - Collection Delegate --
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgDatas.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPathc {
    return CGSizeMake(114, 76);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    BRStationImgCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BRStationImgCollectionViewCell" forIndexPath:indexPath];
    cell.layer.cornerRadius = 4;
    cell.layer.masksToBounds = YES;
    
    NSURL *url = [NSURL URLWithString:self.imgDatas[indexPath.row]];
    
    [cell.img sd_setImageWithURL:url placeholderImage:UIImage.qd_stationDefaultImg];
    
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BRStationImgCollectionViewCell *cell = (BRStationImgCollectionViewCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:0]];
    
    [self clickImg:indexPath.row imageView:cell.img];
}

#pragma mark - SDCycleScrollView delegate --
/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index {
    
//    if (_dotView.isHidden) return;
//    [_dotView selectItem:index];
}

#pragma mark - img picker --
- (void)clickImg:(NSInteger)index imageView:(nonnull UIImageView *)logImgView {
    
    TZImagePickerController *imagePickerVc = [self createTZImagePickerController];
    imagePickerVc.maxImagesCount = 1;
    imagePickerVc.showSelectBtn = NO;
    [imagePickerVc setPhotoPreviewPageDidLayoutSubviewsBlock:^(UICollectionView *collectionView, UIView *naviBar, UIButton *backButton, UIButton *selectButton, UILabel *indexLabel, UIView *toolBar, UIButton *originalPhotoButton, UILabel *originalPhotoLabel, UIButton *doneButton, UIImageView *numberImageView, UILabel *numberLabel) {
        if (naviBar) {
            [naviBar removeFromSuperview];
            naviBar = nil;
        }
        if (backButton) {
            [backButton removeFromSuperview];
            backButton = nil;
        }
        if (numberLabel) {
            [numberLabel removeFromSuperview];
            numberLabel = nil;
        }
        if (toolBar) {
            [toolBar removeFromSuperview];
            toolBar = nil;
        }
        if (numberImageView) {
            [numberImageView removeFromSuperview];
            numberImageView = nil;
        }
        if (doneButton) {
            [doneButton removeFromSuperview];
            doneButton = nil;
        }
    }];
    
    NSArray *urlArr = [self.imgDatas mutableCopy];
    TZImagePreviewController *previewVc = [[TZImagePreviewController alloc] initWithPhotos:urlArr currentIndex:index tzImagePickerVc:imagePickerVc];
    [previewVc setBackButtonClickBlock:^(BOOL isSelectOriginalPhoto) {
        NSLog(@"预览页 返回 isSelectOriginalPhoto:%d", isSelectOriginalPhoto);
    }];
    

    [previewVc setSetImageWithURLBlock:^(NSURL *URL, UIImageView *imageView, NSInteger index, void (^completion)(void)) {
        [self configImageView:imageView URL:URL index:index completion:completion];
    }];
    
    [previewVc setDoneButtonClickBlock:^(NSArray *photos, BOOL isSelectOriginalPhoto) {
        
        NSLog(@"预览页 完成 isSelectOriginalPhoto:%d photos.count:%zd", isSelectOriginalPhoto, photos.count);
    }];
    
    BRWeakObj(previewVc);
    previewVc.updateCurreIndex = ^(NSInteger index) {
        BRStrong(previewVc);
        [previewVc updateTitleString:@""];
        [self->_myCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    };
    
    [self scaleOfCell:logImgView text:@"" VC:previewVc];
    
    WS(weakSelf);
    previewVc.signOutBlock = ^(UIImageView *imageView, NSInteger cureIndex) {
        
        [weakSelf scaleOfVCImage:imageView index:cureIndex cellIndex:0];
    };
}

- (void)scaleOfCell:(UIImageView *)image text:(NSString *)text VC:(TZImagePreviewController *)previewVc{
    
    __block UIImageView *imageView = [[UIImageView alloc] initWithFrame:[image convertRect: image.bounds toView:image.window]];
    imageView.backgroundColor = [UIColor blackColor];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.alpha = 0;
    [self.view addSubview:imageView];
    UIImage *imageN = image.image;
//    if (!imageN) {
//        imageN = UIImage.qd_defaultImg;
//    }
    imageView.image = imageN;
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame = self.view.bounds;
        if (imageView.image.size.height/imageView.image.size.width < kScreenHeight / kScreenWidth) {
            imageView.frame = CGRectMake(0, (kScreenHeight - imageView.image.size.height * (kScreenWidth / imageView.image.size.width)) / 2, kScreenWidth, imageView.image.size.height * (kScreenWidth / imageView.image.size.width));
        }else{
            
            imageView.frame = CGRectMake((kScreenWidth - imageView.image.size.width * (kScreenHeight / imageView.image.size.height)), 0, imageView.image.size.width * (kScreenHeight / imageView.image.size.height), kScreenHeight);
        }
        imageView.alpha = 1;
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
        imageView = nil;
        [self pushViewController:previewVc animated:NO completion:^{
            
            [previewVc updateTitleString:text];
        }];
    }];
}

- (void)scaleOfVCImage:(UIImageView *)image index:(NSInteger)index cellIndex:(NSInteger)cellIndex {
    
    __block UIImageView *imageView = [[UIImageView alloc] initWithFrame:[image convertRect: image.bounds toView:image.window]];
    imageView.frame = self.view.bounds;
    imageView.backgroundColor = [UIColor blackColor];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.alpha = 1;
    [self.view addSubview:imageView];
    imageView.image = image.image;
    [_myCollectionView layoutIfNeeded];//横向布局，图片移动到指定图片后 需layoutIfNeeded，获取cell的准确位置
    BRStationImgCollectionViewCell *cell = (BRStationImgCollectionViewCell *)[_myCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
    
    CGRect frame = [cell.img convertRect:cell.img.bounds toView:self.view];
    [UIView animateWithDuration:0.3 animations:^{

        imageView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
        imageView.alpha = 0;
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
        imageView = nil;
        
    }];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated completion: (void (^)(void))completion {
    [CATransaction setCompletionBlock:completion];
    [CATransaction begin];
    [self.navigationController pushViewController:viewController animated:animated];
    [CATransaction commit];
}

- (void)configImageView:(UIImageView *)imageView URL:(NSURL *)URL index:(NSInteger)index completion:(void (^)(void))completion{
    if ([URL.absoluteString.lowercaseString hasSuffix:@"gif"]) {
        // 先显示静态图占位
        [[SDWebImageManager sharedManager] loadImageWithURL:URL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
            if (!imageView.image) {
                imageView.image = image;
            }
        }];
        // 动图加载完再覆盖掉
        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:URL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            imageView.image = [UIImage sd_imageWithGIFData:data];
            if (completion) {
                completion();
            }
        }];
    } else {
        NSArray *urlArr = [self.imgDatas mutableCopy];
        NSString *url = urlArr[index];
        UIImage *image =  (UIImage *)[[SDImageCache sharedImageCache] imageFromCacheForKey:url];
        if (!image) {
            //展开后没有缓存优先显示默认图片
            image = UIImage.qd_stationDefaultImg;
        }
        [imageView sd_setImageWithURL:URL placeholderImage:image completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (completion) {
                completion();
            }
        }];
//        [imageView sd_setImageWithURL:URL completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//            if (completion) {
//                completion();
//            }
//        }];
    }
}

#pragma mark - TZImagePickerController

- (TZImagePickerController *)createTZImagePickerController {
    [TZImageManager manager].isPreviewNetworkImage = YES;
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 columnNumber:4 delegate:self pushPhotoPickerVc:NO];
    
#pragma mark - 个性化设置，这些参数都可以不传，此时会走默认设置
    imagePickerVc.isSelectOriginalPhoto = NO;
//    imagePickerVc.needShowStatusBar = YES;
//    imagePickerVc.view.backgroundColor = COLOR_WITH_HEX_AND_ALPHA(@"#F5F5F5", 1);
//     imagePickerVc.naviTitleColor = [UIColor greenColor];
//    imagePickerVc.navigationController.title = @"标题";
//    imagePickerVc.naviBgColor = UIColor.redColor;
    // imagePickerVc.oKButtonTitleColorDisabled = [UIColor lightGrayColor];
    // imagePickerVc.oKButtonTitleColorNormal = [UIColor greenColor];
    // imagePickerVc.navigationBar.translucent = NO;
    imagePickerVc.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0];

    imagePickerVc.allowPickingVideo = YES;
    imagePickerVc.allowPickingImage = YES;
    imagePickerVc.allowPickingOriginalPhoto = YES;
    imagePickerVc.allowPickingGif = YES;
    imagePickerVc.showSelectedIndex = YES;
    imagePickerVc.allowPickingMultipleVideo = YES;

    // imagePickerVc.minImagesCount = 3;
    // imagePickerVc.alwaysEnableDoneBtn = YES;
    imagePickerVc.needShowStatusBar = YES;
    imagePickerVc.showSelectBtn = NO;
    //imagePickerVc.allowPreview = NO;
    // imagePickerVc.preferredLanguage = @"zh-Hans";

#pragma mark - 到这里为止
    
    return imagePickerVc;
}

#pragma mark - SET UI --
- (void)setUI {
    
    CGFloat marginX = 20;
    CGFloat width = kScreenWidth - 20;
    CGFloat height = kScreenWidth * 2 / 3;//kScale414(264.0);
    self.view.backgroundColor = UIColor.qd_backgroundColor;
//    UIImageView *headImg  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height)];
//    headImg.image = UIImage.qd_stationDefaultImg;
    
    
    
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.backgroundColor = UIColorClear;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [scrollView insertSubview:self.sdCyView atIndex:0];
    [self.sdCyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(0);
        make.height.mas_equalTo(height);
        make.width.mas_equalTo(kScreenWidth);
    }];
    
    CGFloat bottomV_h = 84 + kSafeAreaBottomHeight;
    // bottom = 84 - 25 + kSafeAreaBottomHeight
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = UIColor.qd_backgroundColor;
    contentView.layer.cornerRadius = 25.;
    contentView.layer.masksToBounds  = YES;
    [scrollView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(scrollView).offset(-(bottomV_h - 25));
        make.top.equalTo(scrollView).offset((height - 25));
        make.width.mas_equalTo(kScreenWidth);
        make.left.equalTo(scrollView);
    }];
    _contentView = contentView;
    
    
    QMUIMarqueeLabel *titleLabel = [[QMUIMarqueeLabel alloc] init];
    titleLabel.font = [UIFont fontB:20];
    titleLabel.textColor = UIColor.qd_titleTextColor;
    titleLabel.text = @"--";
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.shouldFadeAtEdge = NO;
    [contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(62);
        make.left.equalTo(contentView.mas_left).offset(marginX);
        make.right.equalTo(contentView.mas_right).offset(-marginX);
    }];
    _titleL = titleLabel;
    
    UIView *line4 = [self getLineView];
    [contentView addSubview:line4];
    [line4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(0.5);
        make.centerX.equalTo(contentView);
        make.top.equalTo(contentView.mas_top).offset(62);
    }];
    
    UIView *avaiableBGView = [[UIView alloc] init];
    avaiableBGView.backgroundColor = UIColorClear;
    [contentView addSubview:avaiableBGView];
    [avaiableBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth);
        make.height.mas_equalTo(44);
        make.top.equalTo(line4.mas_bottom);
        make.left.equalTo(contentView);
    }];
    
    QMUILabel *avaiableL = [[QMUILabel alloc] init];
    avaiableL.font = [UIFont fontS:14];
    avaiableL.textColor = kColorMain;
    avaiableL.text = [NSString stringWithFormat:@"%@:-",Localized(@"home_rent_tag")];
    [avaiableBGView addSubview:avaiableL];
    [avaiableL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(avaiableBGView.mas_centerY).offset(0);
        make.left.equalTo(avaiableBGView.mas_left).offset(marginX);
    }];
    
    QMUILabel *emptyL = [[QMUILabel alloc] init];
    emptyL.font = [UIFont fontS:14];
    emptyL.textColor = kColorMain;
    emptyL.text = [NSString stringWithFormat:@"%@:-",Localized(@"home_return_tag")];
    [avaiableBGView addSubview:emptyL];
    [emptyL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(avaiableBGView.mas_centerY).offset(0);
        make.left.equalTo(avaiableL.mas_right).offset(16);
    }];
    
    
    
    UIView *timeTopLine = [self getLineView];
    [contentView addSubview:timeTopLine];
    [timeTopLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(0.5);
        make.centerX.equalTo(contentView);
        make.top.equalTo(avaiableBGView.mas_bottom).offset(0);
    }];
    
    UIView *timeView = [UIView new];
    [contentView addSubview:timeView];
    [timeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth);
        make.height.mas_equalTo(52);
        make.top.equalTo(timeTopLine.mas_bottom);
        make.left.equalTo(contentView);
    }];
    
    QMUILabel *timeL = [[QMUILabel alloc] init];
    timeL.textColor = UIColor.qd_titleTextColor;
    timeL.numberOfLines = 1;
    timeL.font = [UIFont fontM:14];
    [timeView addSubview:timeL];
    [timeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(marginX);
        make.centerY.offset(0);
    }];
    
    UIView *line = [self getLineView];
    [contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(0.5);
        make.centerX.equalTo(contentView);
        make.top.equalTo(timeView.mas_bottom).offset(0);
    }];
    
    UIView *locBGView = [[UIView alloc] init];
    locBGView.backgroundColor = UIColorClear;
    [contentView addSubview:locBGView];
    [locBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth);
//        make.height.mas_equalTo(52);
        make.top.equalTo(line.mas_bottom);
        make.left.equalTo(contentView);
    }];
    
    QMUILabel *locLabel = [[QMUILabel alloc] init];
    locLabel.textColor = kColorTitle;
    locLabel.numberOfLines = 0;
    locLabel.font = [UIFont fontM:14];
    [locBGView addSubview:locLabel];
    [locLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(14);
        make.left.offset(marginX);
        make.right.offset(-marginX);
        make.bottom.offset(-14);
    }];
    locLabel.text = @"--";
    
    UIView *line2 = [self getLineView];
    [contentView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(0.5);
        make.centerX.equalTo(contentView);
        make.top.equalTo(locBGView.mas_bottom).offset(0);
    }];
//    _line2 = line2;
//
//    UIView *photoView = [[UIView alloc] init];
//    photoView.clipsToBounds = YES;
//    [contentView addSubview:photoView];
//    [photoView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(line2).offset(15);
//        make.left.right.equalTo(contentView);
//        make.height.mas_equalTo(125);
//    }];
//    _photoView = photoView;
//
//    QMUILabel *titleLabel2 = [[QMUILabel alloc] init];
//    titleLabel2.font = [UIFont br_fontB:17];
//    titleLabel2.textColor = UIColor.qd_titleTextColor;
//    titleLabel2.text = Localized(@"Station_storePhotos");
//    [photoView addSubview:titleLabel2];
//    [titleLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(photoView).offset(0);
//        make.left.equalTo(photoView.mas_left).offset(marginX);
//        make.right.equalTo(photoView.mas_right).offset(-marginX);
//    }];
//
//    //图片
//    UICollectionViewFlowLayout *layout  = [[UICollectionViewFlowLayout alloc] init];
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    UICollectionView *imgCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
//    [photoView addSubview:imgCollection];
//    imgCollection.delegate = self;
//    imgCollection.dataSource = self;
//    [imgCollection mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(photoView).offset(marginX);
//        make.top.equalTo(titleLabel2.mas_bottom).offset(8);
//        make.right.equalTo(photoView);
//        make.height.mas_equalTo(76);
//    }];
//    [imgCollection registerNib:[UINib nibWithNibName:NSStringFromClass([BRStationImgCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"BRStationImgCollectionViewCell"];
//    imgCollection.backgroundColor = UIColor.qd_backgroundColor;
//    imgCollection.showsHorizontalScrollIndicator = NO;
//    _myCollectionView = imgCollection;
//
//    UIView *line3 = [self getLineView];
//    [contentView addSubview:line3];
//    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(width);
//        make.height.mas_equalTo(0.5);
//        make.centerX.equalTo(contentView);
//        make.top.equalTo(line2.mas_bottom).offset(140);
//    }];
//    _line3 = line3;
    // 联系方式视图（替换计费规则）
    UIView *contactView = [[UIView alloc] init];
    [contentView addSubview:contactView];
    [contactView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line2).offset(15);
        make.left.equalTo(contentView.mas_left).offset(marginX);
        make.right.equalTo(contentView.mas_right).offset(-marginX);
        make.bottom.equalTo(contentView).offset(-40);
    }];
    
    QMUILabel *contactTitleLabel = [[QMUILabel alloc] init];
    contactTitleLabel.font = [UIFont fontS:17];
    contactTitleLabel.textColor = kColorTitle;
    contactTitleLabel.text = Localized(@"station_contactInfo");
    [contactView addSubview:contactTitleLabel];
    [contactTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
    }];
    
    QMUILabel *contactLabel = [[QMUILabel alloc] init];
    contactLabel.font = [UIFont fontM:14];
    contactLabel.textColor = kColorTitle;
    contactLabel.text = @"--";
    [contactView addSubview:contactLabel];
    [contactLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contactTitleLabel.mas_bottom).offset(12);
        make.left.right.bottom.offset(0);
    }];
    _contactLabel = contactLabel;
    
    [contentView  layoutIfNeeded];

    //返回
    QMUIButton *backBtn = [[QMUIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [backBtn setImage:UIImageMake(@"station_back") forState:UIControlStateNormal];
    [self.view addSubview:backBtn];
    [backBtn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
        make.top.equalTo(self.view).offset(KStatusBarHeight);
        make.left.equalTo(self.view.mas_left).offset(16);
    }];
    
//    _headImg = headImg;
    _locL = locLabel;
    _availableL = avaiableL;
    _emptyL = emptyL;
    _timeLabel = timeL;
    
    /// bottomView
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = UIColor.qd_backgroundColor;
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(bottomV_h);
        make.right.left.bottom.offset(0);
    }];
    
//    UIView *bottomLine = [self getLineView];
//    [bottomView addSubview:bottomLine];
//    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.right.offset(0);
//        make.height.mas_equalTo(0.5);
//    }];
    
    UIStackView *stackView = [[UIStackView alloc] initWithFrame:CGRectMake(28, 16, kScreenWidth - 56, 52)];
    stackView.spacing = 16;
    stackView.axis = UILayoutConstraintAxisHorizontal;
    [bottomView addSubview:stackView];
    
    
    QMUIButton *locBtn = [BRToolsView br_btnBGImg:@"--m"];
    [locBtn setImage:UIImageMake(@"station_location") forState:UIControlStateNormal];
    locBtn.imagePosition = QMUIButtonImagePositionRight;
    locBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [stackView addArrangedSubview:locBtn];
    
    [locBtn addTarget:self action:@selector(toMap) forControlEvents:UIControlEventTouchUpInside];
    _locBtn = locBtn;
    _stackView = stackView;
}

- (UIView *)getLineView {
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = UIColor.qd_lineColor;
    return line;
}

- (SDCycleScrollView *)sdCyView {
    if (_sdCyView == nil) {
        _sdCyView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, 94) delegate:self placeholderImage:UIImageMake(kDefaultImg)];
        _sdCyView.autoScroll = YES;
        _sdCyView.showPageControl = NO;
//        _sdCyView.imageURLStringsGroup = @[@"1",@"2",@"3"];
//        _sdCyView.layer.cornerRadius = kCorners;
//        _sdCyView.layer.masksToBounds = YES;
    }
    return _sdCyView;
}

- (BOOL)preferredNavigationBarHidden {
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
